//
//  PSFileListNode.m
//  photoSep
//
//  Created by David Michaeli on 05/10/12.
//  Copyright (c) 2012 David Michaeli. All rights reserved.
//

#import "PSFileListNode.h"
#import "NSImage+Thumbnail.h"
#import <cv.hpp>
#import <cvaux.h>
#import <highgui.h>

@implementation PSFileListNode

- (id)init
{
    self = [super init];
    if (self)
    {
        _childrenImages = [[NSMutableArray alloc] init];
        _name = [[NSString alloc] initWithString:@"Untitled"];
    }
    return self;
}

- (id)initWithName:(NSString*)newname
{
    [self init];
    _name = [newname copy];
    
    [NSThread detachNewThreadSelector:@selector(processingMainMethod:) toTarget:self withObject:self];
    return self;
}

- (id)initWithPath:(NSString*)filePath
{
    [self init];
    _name = [filePath lastPathComponent];
    _url = [[NSURL alloc] initFileURLWithPath:filePath];
    
    [NSThread detachNewThreadSelector:@selector(processingMainMethod:) toTarget:self withObject:self];
    return self;
}


// ======================================================
// Processing Thread
- (void)processingMainMethod:(id)node
{
    PSFileListNode* curNode = (PSFileListNode*)node;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSNumber *prog = [NSNumber numberWithDouble:0.0];
    
    [curNode setProcessingThread:[NSThread currentThread]];
    
    NSString *file_path = [[curNode url] path];
    CvMat *mat = cvLoadImageM([file_path UTF8String], 1);
    
    cvSobel(mat, mat, 1, 1, 1);
    _image = [[NSImage alloc] initWithCVMat:*mat];
    
    cvDecRefData(mat);
    
    [NSThread sleepForTimeInterval:1];
    prog = [NSNumber numberWithDouble:50.0];
    [self performSelectorOnMainThread:@selector(setProgress:) withObject:[prog retain] waitUntilDone:NO];
    [NSThread sleepForTimeInterval:1];
    
    prog = [NSNumber numberWithDouble:100.0];
    [self performSelectorOnMainThread:@selector(setProgress:) withObject:[prog retain] waitUntilDone:NO];
    
    [prog release];
    [pool release];
}
// ======================================================



- (void)setProgress:(NSNumber*)num
{
    _processProgress = [num doubleValue];
    [_selectorTarget performSelector:_setProgressSelector withObject:self];
}

- (void)dealloc
{
    [_name release];
    [_childrenImages release];
    [super dealloc];
}

@synthesize name = _name;
@synthesize image = _image;
@synthesize childrenImages = _childrenImages;
@synthesize removeButtonSelector = _removeButtonSelector;
@synthesize setProgressSelector = _setProgressSelector;
@synthesize selectorTarget = _selectorTarget;
@synthesize removeButtonSource = _removeButtonSource;
@synthesize processProgress = _processProgress;
@synthesize processingThread = _processingThread;
@synthesize url = _url;


- (NSComparisonResult)compare:(id)otherNode {
    if ([otherNode isKindOfClass:[PSFileListNode class]]) {
        PSFileListNode *other = (PSFileListNode *)otherNode;
        return [self.name compare:[other name]];
    } else {
        return NSOrderedAscending;
    }
}

@end
